﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace OBDUDSUtils.Common
{
    /// <summary>
    /// 用于发送或接收报文的保存
    /// </summary>
    public class DataFile
    {
        private static string faultDataSavePath = "Data\\Fault\\";
        public static string defaultDir = "PVEData";
        public static string obdDataPath = "PVEData\\OBD\\";
        public static string autoTestPath = "PVEData\\autoTest";
        private static string testLogPath = "Log\\";
        private static object fileObj = new object();

        public static int Data_SaveMessage(string faultContent, string savePath)
        {
            try
            {
                lock (fileObj)
                {
                    if (faultContent == "")
                        return 0;
                    using (FileStream fs = new FileStream(savePath, FileMode.Append, FileAccess.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            sw.WriteLine(faultContent);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log.Error(ex.Message + ex.StackTrace);
            }
            return 1;
        }

        public static string GetFaultSavePath()
        {
            var savePath = System.Environment.CurrentDirectory + "\\" + faultDataSavePath;
            if (!Directory.Exists(savePath))
                Directory.CreateDirectory(savePath);
            savePath += "faultData_" + System.DateTime.Now.ToString("yyyyMMddHH") + ".txt";
            return savePath;
        }

        public static string GetTestLogSavePath()
        {
            var savePath = System.Environment.CurrentDirectory + "\\" + testLogPath;

            if (!Directory.Exists(savePath))
                Directory.CreateDirectory(savePath);
            savePath += "log_" + System.DateTime.Now.ToString("yyyyMMddHH") + ".txt";
            return savePath;
        }

        public static string GetOBDSavePath(string directory)
        {
            if (string.IsNullOrEmpty(directory))
                directory = System.Environment.CurrentDirectory + "\\" + obdDataPath + "\\";
            if (!directory.EndsWith("\\\\"))
                directory += "\\";
            directory += obdDataPath + "\\";

            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);
            return directory + "OBDData_" + System.DateTime.Now.ToString("yyyyMMddHH") + ".txt";
        }

        public static string GetAutoTestSavePath(string directory)
        {
            if (string.IsNullOrEmpty(directory))
                directory = System.Environment.CurrentDirectory + "\\data\\" + autoTestPath + "\\";
            if (!directory.EndsWith("\\\\"))
                directory += "\\";
            directory += autoTestPath + "\\";
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);

            return directory + "OBDData_" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";
        }
    }
}
